package cn.gailvlun.gll.presentation.chat

import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityModifyGroupNameBinding
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.apartment.room.Room
import cn.gailvlun.gll.net.group.ModifyGroupReq
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.chat.vm.ModifyGroupNameViewModel
import cn.gailvlun.gll.util.DialogUtil
import cn.gailvlun.gll.util.RxUtil
import cn.gailvlun.gll.util.ToastUtil
import io.reactivex.Observable
import kotlinx.android.synthetic.main.activity_modify_group_name.*
import rx_activity_result2.Result
import rx_activity_result2.RxActivityResult

class ModifyGroupNameActivity: BaseActivity() {

    companion object {
        fun openActivity(context: Activity, roomId: Int): Observable<Result<Activity>> {
            return RxActivityResult.on(context).startIntent(Intent(context, ModifyGroupNameActivity::class.java).apply {
                putExtra("roomId", roomId)
            })
        }
    }

    private lateinit var mBinding: ActivityModifyGroupNameBinding

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_modify_group_name)
    }

    override fun createHandler() {
        nbv.setOnBackListener {
            onBackPressed()
        }
        nbv.setOnMenuClickListener {
            val modifiedName = mBinding.etNickName.text.toString()
            HttpMethods.getGroupService()
                    .modifyGroupName(ModifyGroupReq().apply {
                        room_id = roomId
                        name = modifiedName
                    })
                    .compose(RxUtil.applyScheduler())
                    .compose(bindToLifecycle())
                    .subscribe(object: HttpSubscriber<Room>() {
                        override fun onSuccess(response: Room) {
                            ToastUtil.showCenter("修改成功")
                            
                            mViewModel.modifyRoomName(modifiedName, mRoom!!)

                            val intent = Intent()
                            intent.putExtra("roomName", response.name)
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                        override fun onFailure(errMsg: String?, response: Room?, code: Int) {
                            DialogUtil.showError(mContext, errMsg)
                        }
                    })
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

    private lateinit var mViewModel: ModifyGroupNameViewModel

    private var roomId: Int? = null
    private var mRoom: Room? = null

    override fun loadData(savedInstanceState: Bundle?) {
        roomId = intent.getIntExtra("roomId", -1).apply {
            if (this == -1) {
                finish()
                return
            }
        }

        mViewModel = ViewModelProviders.of(this).get(ModifyGroupNameViewModel::class.java)
        mRoom = mViewModel.getRoomBy(roomId!!).apply {
            if (this == null) {
                finish()
                return
            }
        }

        mBinding.item = mRoom

        mBinding.etNickName.setText(mBinding.item?.name)
        mBinding.etNickName.setSelection(mBinding.etNickName.text.length)
    }

}